package by.twouexplorer;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.app.Activity;
import android.view.View.OnClickListener;
import android.os.Bundle;

public class Adapter extends BaseAdapter {
	
	private LayoutInflater inflater;
	private List<MetaData> items = new ArrayList<MetaData>();
	
	public Adapter(Context context) {
		inflater = LayoutInflater.from(context);
	}
	
	public void setItems(List<MetaData> items) {
		this.items = items;
		notifyDataSetChanged();
	}

	public int getCount() {
		return items.size();
	}

	public Object getItem(int position) {
		return items.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = newView(position, parent);
		}
		bindView(position, convertView);
		
		return convertView;
	}
	
	private View newView(int postion, ViewGroup parent) {
		View view = inflater.inflate(R.layout.list_item, parent, false);
		ViewHolder holder = getHolder(view);
		view.setTag(holder);
		return view;
	}
	
	private void bindView(int position, View conveView) {
		ViewHolder holder = (ViewHolder) conveView.getTag();
		File file = new File (((MetaData)getItem(position)).getPath());
		holder.textView.setText(items.get(position).getName());
		holder.imageView.setImageResource(getIcon(file));
	}
	
	private ViewHolder getHolder(View view) {
		ViewHolder holder = new ViewHolder();
		holder.imageView = (ImageView) view.findViewById(R.id.image);
		holder.textView = (TextView) view.findViewById(R.id.text);
		return holder;
	}
	
	class ViewHolder {
		public ImageView imageView;
		public TextView textView;
	}
	
	static boolean isMusic(File file) {
		
		String fileName = file.getName();
		return (fileName.endsWith(".mp3") || fileName.endsWith(".aac") || fileName.endsWith(".ogg") || fileName.endsWith(".wav") || fileName.endsWith(".m4a") || fileName.endsWith(".wma") || fileName.endsWith(".mra"));
	}
	
	static boolean isPicture(File file) {
		
		String fileName = file.getName();
		return (fileName.endsWith(".jpg") || fileName.endsWith(".png") || fileName.endsWith(".bmp") || fileName.endsWith(".gif"));
	}
	
	static boolean isProtected(File path)
	{
		return (!path.canRead() && !path.canWrite());
	}

	static int getIcon(File file) {
		
		if(file.isDirectory()) 
		{
			if(isProtected(file))
			{
				return R.drawable.folder_protect;
			}
			else 
			{
				return R.drawable.folder;
			}
		}
		else 
		{
			//String fileName = file.getName();
			if(isMusic(file))
			{
				return R.drawable.music;
			}
			else if(isPicture(file))
			{
				return R.drawable.image;
			}
			else
			{
				return R.drawable.general;
			}
		}
		
	}
	

}
